<?php

//apf_require_class('Aifang_Core_Cache_Factory');

class Util_Chinese2Pinyin {
    private $memcache;
    private $py_table;

    public function __construct() {
        require_once("pinyin_table.php");
        $this->py_table = $pinyin_table;
    }

    public function get_pinyin_first($string) {
        $flow = $this->get_pinyin_array($string);
        if (empty($flow) || (isset($flow) && !is_array($flow))) {
            return array('str' => '', 'isdyz' => false);
        }

        $str = '';
        $isduoyinzi = false;
        foreach ($flow as $py) {
            if (is_array($py)) {
                $d = count($py);
                if ($d > 0) {
                    $pyc = $py[0];
                    $num = intval($pyc);
                    if ($num <= 0) {
                        $str .= substr($pyc, 0, 1);
                    } else {
                        if (($num >= ord('a') && $num <= ord('z'))
                            || ($num <= ord('Z') && $num >= ord('A'))
                            || ($num <= ord('9') && $num >= ord('0'))) {
                            $str .= chr($num);
                        }
                    }

                    if ($d > 1) {
                        $isduoyinzi = true;
                    }
                }
            } else if (intval($py) > 0) {
                $num = intval($py);
                if (($num >= ord('a') && $num <= ord('z'))
                    || ($num <= ord('Z') && $num >= ord('A'))
                    || ($num <= ord('9') && $num >= ord('0'))) {
                    $str .= chr($num);
                }
            }
        }
        $str = strtoupper($str);
        return array('str' => $str, 'isdyz' => $isduoyinzi);
    }

    public function get_pinyin_all($string) {
        $flow = $this->get_pinyin_array($string);
        if (empty($flow) || (isset($flow) && !is_array($flow))) {
            return array('str' => '', 'str_all' => '', 'isdyz' => false);
        }
        $str = '';
        $str_all = '';
        $isduoyinzi = false;
        foreach ($flow as $py) {
            if (is_array($py)) {
                $d = count($py);
                if ($d > 0) {
                    $pyc = $py[0];
                    $str_all .= $py[0];

                    $num = intval($pyc);
                    if ($num <= 0) {
                        $str .= substr($pyc, 0, 1);
                    } else {
                        if (($num >= ord('a') && $num <= ord('z'))
                            || ($num <= ord('Z') && $num >= ord('A'))
                            || ($num <= ord('9') && $num >= ord('0'))) {
                            $str .= chr($num);
                        }
                    }
                    if ($d > 1) {
                        $isduoyinzi = true;
                    }
                }
            } else if (intval($py) > 0) {
                $num = intval($py);
                if (($num >= ord('a') && $num <= ord('z'))
                    || ($num <= ord('Z') && $num >= ord('A'))
                    || ($num <= ord('9') && $num >= ord('0'))) {
                    $str .= chr($num);
                    $str_all .= chr($num);
                }
            }
        }
        $str = strtoupper($str);
        $str_all = strtoupper($str_all);
        return array('str' => $str, 'str_all' => $str_all, 'isdyz' => $isduoyinzi);
    }

    private function get_pinyin_array($string) {
        if (empty($string)) return $string;

        $string = iconv('utf-8', 'gb2312', $string);
        $pinyin_table = $this->py_table;
        $flow = array();
        for ($i=0;$i<strlen($string);$i++) {
            if (ord($string[$i]) >= 0x81 and ord($string[$i]) <= 0xfe) {
                $h = ord($string[$i]);
                if (isset($string[$i+1])) {
                    $i++;
                    $l = ord($string[$i]);
                    if (isset($pinyin_table[$h][$l])) {
                        array_push($flow,$pinyin_table[$h][$l]);
                    } else {
                        array_push($flow,$h);
                        array_push($flow,$l);
                    }
                } else {
                    array_push($flow,ord($string[$i]));
                }
            } else {
                array_push($flow,ord($string[$i]));
            }
        }

        return $flow;

//		$pinyin = array();
//		$pinyin[0] = '';
//		for ($i=0;$i<sizeof($flow);$i++) {
//			if (is_array($flow[$i])) {
//				if (sizeof($flow[$i]) == 1) {
//					foreach ($pinyin as $key => $value) {
//						$pinyin[$key] .= "_".$flow[$i][0]."_";
//					}
//				}
//				if (sizeof($flow[$i]) > 1) {
//					$tmp1 = $pinyin;
//					foreach ($pinyin as $key => $value) {
//						$pinyin[$key] .= "_".$flow[$i][0]."_";
//					}
//					for ($j=1;$j<sizeof($flow[$i]);$j++) {
//						$tmp2 = $tmp1;
//						for ($k=0;$k<sizeof($tmp2);$k++) {
//							$tmp2[$k] .= "_".$flow[$i][$j]."_";
//						}
//						array_splice($pinyin,sizeof($pinyin),0,$tmp2);
//					}
//				}
//			} else  {
//				foreach ($pinyin as $key => $value) {
//					$pinyin[$key] .= chr($flow[$i]);
//				}
//			}
//		}
//		return $pinyin;
    }
}
?>
